using System;
using System.Collections.Generic;
using DecisionMaking.Domain;

namespace DecisionMaking.BusinessLogic.Algorithms
{
    public class TallyingAlgorithm : IAlgorithm
    {
        private AlgorithmDefinition _algorithmDefinition;

        public AlgorithmResult Execute(Agent agent, IList<Cue> cues, Candidate left, Candidate right)
        {
            short result;
            var cuesUsed = (short) cues.Count;
            short candidatesRecognized = 0;

            var leftScore = 0;
            var rightScore = 0;

            if (agent.Recognizes(left))
            {
                leftScore++;
                candidatesRecognized++;
            }

            if (agent.Recognizes(right))
            {
                rightScore++;
                candidatesRecognized++;
            }

            for (var i = 0; i < cuesUsed; i++)
            {
                var leftValue = agent.GetCueValue(left, cues[i]);
                var rightValue = agent.GetCueValue(right, cues[i]);

                if (leftValue != null && leftValue > 0) leftScore++;
                if (rightValue != null && rightValue > 0) rightScore++;
            }

            if (leftScore > rightScore)
                result = -1;
            else if (rightScore > leftScore)
                result = 1;
            else
                result = 0;

            return new AlgorithmResult(result, cuesUsed, candidatesRecognized);
        }

        public string Validate()
        {
            return null;
        }

        public AlgorithmDefinition GetDefinition()
        {
            return _algorithmDefinition;
        }

        public void SetDefinition(AlgorithmDefinition definition)
        {
            _algorithmDefinition = definition;
        }
    }
}